Hash是用來儲存多組欄位值,可以是數字或字串.使用者可以對值進行操作,跟資料結構中的dictionary概念很像.
可用指令
資料結構
設定hash可以有多個field and value 組成.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
# 底層資料結構為ziplist
127.0.0.1:6379> object encoding books
"ziplist"
取得hash中所有的field.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hkeys books
1) "name"
2) "price"
取得hash中所有的value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hvals books
1) "abc"
2) "10"
取得hash中所有的field and value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
取得hash中指定field的value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hget books name
"abc"
127.0.0.1:6379> hget books price
"10"
127.0.0.1:6379> hget books no
(nil)
設定hash中指定的field的value依照設定數值進行遞增.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hincrby books price 1
(integer) 11
127.0.0.1:6379> hget books price
"11"
設定hash中指定的field的value依照設定浮點數值進行遞增.
127.0.0.1:6379> hset books name abc price 10 discountrate 0.2
(integer) 3
127.0.0.1:6379> hincrbyfloat books discountrate 0.5
"0.7"
127.0.0.1:6379> hget books discountrate
"0.7"
刪除hash中指定field.
127.0.0.1:6379> hset books name abc price 10 discountrate 0.2
(integer) 3
127.0.0.1:6379> hget books discountrate
"0.7"
127.0.0.1:6379> hdel books discountrate
(integer) 1
127.0.0.1:6379> hget books discountrate
(nil)
取得hash中多個指定field的value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hmget books name price
1) "abc"
2) "10"
設定hash中key的多個指定field的value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hmget books name price
1) "abc"
2) "10"
127.0.0.1:6379> hmset books name def price 33
OK
127.0.0.1:6379> hmget books name price
1) "def"
2) "33"
設定hash中key的field和value但需要不存在既有的fiels時才寫入field與value.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
# 目前設定books所有的field and value
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
# 需要設定books中field name的value會失敗
127.0.0.1:6379> hsetnx books name def
(integer) 0
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
# 需要設定books中field discountrate的value因此field不存在所以成功
127.0.0.1:6379> hsetnx books discountrate 0.2
(integer) 1
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
5) "discountrate"
6) "0.2"
確認hash中key指定的field是否存在.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hgetall books
1) "name"
2) "abc"
3) "price"
4) "10"
# books中的field name 存在
127.0.0.1:6379> hexists books name
(integer) 1
# books中的field discountrate 不存在
127.0.0.1:6379> hexists books discountrate
(integer) 0
取得hash key中的field個數.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hlen books
(integer) 2
取得hash key中指定field的value字串長度.
127.0.0.1:6379> hset books name abc price 10
(integer) 2
127.0.0.1:6379> hstrlen books name
(integer) 3
掃描hash key中指定field 特定match pattern與次數.
# 存在name
127.0.0.1:6379> hscan books 0 match name count 1
1) "0"
2) 1) "name"
2) "abc"
# 不存在discountrate
127.0.0.1:6379> hscan books 0 match discountrate
1) "0"
2) (empty array)
Hash優點
Hash缺點